//
//  YCHomeViewController.m
//  YCVillageDoctor
//
//  Created by zhangxiaoliang on 2017/6/7.
//  Copyright © 2017年 zhangxiaoliang. All rights reserved.
//

#import "YCHomeViewController.h"
#import "UnRegisterView.h"
#import "ApplyMemberViewController.h"
#import "MembershipViewController.h"
#import "YCLoginViewController.h"
#import "MyProfileModel.h"


#define Margen (kDevice_iPhone5?50:80)
@interface YCHomeViewController ()

@property (nonatomic, strong) UnRegisterView * unregisterView;
@property (nonatomic ,strong)MyProfileModel *profile;

@end

@implementation YCHomeViewController

- (UnRegisterView *)unregisterView
{
    if (!_unregisterView)
    {
        self.unregisterView = [[UnRegisterView alloc] initWithCardFrame:CGRectMake(50, 150, SCREEN_WIDTH - 100, 350)];
        
    }
    
    return _unregisterView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //1.初始化UI
    [self stupUI];
    
    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(Authentication:) name:@"HomeToAuthentication" object:nil];
    
#pragma mark - 获取专家信息
    NSString *expert_id= [YCUserTool sharedTools].expertID;
    
    NSDictionary  *params = @{
                              @"expert_id":judgeString(expert_id)
                              };
    
    [DownloadData post:kGetMineInfo params:params success:^(id json) {
        
        if (json) {
            self.profile = [MyProfileModel mj_objectWithKeyValues:json];
            NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
            
            [def setObject:self.profile.checkflag forKey:KCHECKFLAG];
            [def setObject:self.profile.hosName forKey:KHOSNAME];
            [def setObject:self.profile.expertName forKey:KEXPERTNAME];
            
        }
    } failure:^(NSError *error) {
        
    }];
    
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
     self.navigationController.navigationBar.hidden = YES;
}

//-(void)dealloc{
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
//}

- (void)stupUI
{
    
    UIImageView *imageBg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"homebg"]];
    imageBg.frame = self.view.bounds;
    
    [self.view addSubview:imageBg];
    
    
    UILabel *labelTitle = [[UILabel alloc]init];
    labelTitle.text = @"首页";
    labelTitle.textAlignment = NSTextAlignmentCenter;
    labelTitle.font = [UIFont systemFontOfSize:20];
    labelTitle.textColor = [UIColor whiteColor];
    [self.view addSubview:labelTitle];
    
    
    
    UIButton *btnSellCard = [[UIButton alloc]init];
    [btnSellCard addTarget:self action:@selector(btnSellCardClick) forControlEvents:UIControlEventTouchUpInside];
    [btnSellCard setAdjustsImageWhenHighlighted:NO];
    [btnSellCard setImage:[UIImage imageNamed:@"组-1"] forState:UIControlStateNormal];
    [self.view addSubview:btnSellCard];
    
    
    UILabel *slabel = [[UILabel alloc]init];
    slabel.text = @"售卡";
    [self.view addSubview:slabel];
    
    
    
    
    UIButton *btnReading = [[UIButton alloc]init];
    [btnReading setImage:[UIImage imageNamed:@"组-2"] forState:UIControlStateNormal];
    [btnReading setAdjustsImageWhenHighlighted:NO];
    [btnReading addTarget:self action:@selector(btnReadingClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnReading];
    
    
    UILabel *rlabel = [[UILabel alloc]init];
    rlabel.text = @"阅片";
    [self.view addSubview:rlabel];
    
    
    __weak __typeof(&*self)mySelf = self;
    
    [labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(mySelf.view.mas_top).mas_offset(20);
        make.centerX.mas_equalTo(mySelf.view.mas_centerX);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(40);
        
    }];
    
    [btnSellCard mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(mySelf.view.mas_top).mas_offset(Margen+20);
        make.centerX.mas_equalTo(mySelf.view.mas_centerX);
        make.width.mas_equalTo(170);
        make.height.mas_equalTo(170);
        
    }];
    
    
    [slabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(btnSellCard.mas_bottom).mas_offset(8);
        make.centerX.mas_equalTo(mySelf.view.mas_centerX);
        make.height.mas_equalTo(40);
    }];
    
    
    [btnReading mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.mas_equalTo(rlabel.mas_top).mas_offset(-15);
        make.centerX.mas_equalTo(mySelf.view.mas_centerX);
        make.width.mas_equalTo(170);
        make.height.mas_equalTo(170);
        
    }];
    
    [rlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.mas_equalTo(mySelf.view.mas_bottom).mas_offset(-Margen);
        make.centerX.mas_equalTo(mySelf.view.mas_centerX);
        make.height.mas_equalTo(40);
    }];
    
}

//-(void)Authentication:(NSNotification *)notification{
//    
//   [self toAuthentication];
//    
//}

- (void)btnSellCardClick
{
//    NSUserDefaults *def=  [NSUserDefaults standardUserDefaults];
//    NSString *expert_id= [def objectForKey:KEXPERTID];
//    NSString * check_flag = [def objectForKey:KCHECKFLAG];
//    
//    if (expert_id && [check_flag isEqualToString:@"1"]) {
    
        self.tabBarController.selectedIndex = 1;
        
//    }else{
//        
//        [self toAuthentication];
//    }
//    
}

- (void)btnReadingClick
{
    
//    NSUserDefaults *def=  [NSUserDefaults standardUserDefaults];
//    NSString *expert_id= [def objectForKey:KEXPERTID];
//    NSString * check_flag = [def objectForKey:KCHECKFLAG];
//    
//    if (expert_id && [check_flag isEqualToString:@"1"]) {
    
        self.tabBarController.selectedIndex = 2;
//    }else{
//    
//         [self toAuthentication];
//    }
}

//-(void)toAuthentication{

//    NSUserDefaults *def=  [NSUserDefaults standardUserDefaults];
//    NSString *expert_id= [def objectForKey:KEXPERTID];
//    NSString * check_flag = [def objectForKey:KCHECKFLAG];
//    
//    if  ( expert_id.length !=0  ){
//        
//        if ([check_flag  isEqualToString:@"3"])
//        {
//            [self.unregisterView showCard];
//            weakify(self);
//            self.unregisterView.gotoRegist = ^{
//                
//                
//                strongify(self);
//                ApplyMemberViewController *appVC = [[ApplyMemberViewController alloc]init];
//                appVC.popType = MEMBER_SHIP_POP_TYPE_POP;
//                [self presentViewController:appVC animated:YES completion:nil];
//                
//                
//            };
//            
//        } else if ([check_flag isEqualToString:@"0"]){
//            
//            MembershipViewController   *follVC = [[MembershipViewController alloc]init];
//            follVC.profile=self.profile;
//            [self.navigationController pushViewController:follVC animated:YES];
//            
//            
//        }else if ([check_flag isEqualToString:@"2"]){
//            
//            [UIAlertController alertTipWithTitle:@"您的资料有问题或资料不全" message:@"会员可以享受更多功能，请您再一次提交准确资料！" sure:^(UIAlertAction *action) {
//                
//                ApplyMemberViewController *appVC = [[ApplyMemberViewController alloc]init];
//                appVC.popType = MEMBER_SHIP_POP_TYPE_POP;
//                
//                [self presentViewController:appVC animated:YES completion:nil];
//                
//            } cancel:^(UIAlertAction *action) {
//                
//            }];
//        }
//        
//    }else{
//        
//        
//        
//        NSString *expertId = [[NSUserDefaults standardUserDefaults] objectForKey:KEXPERTID];
//        if (expertId.length==0) {
//            
//            [UIAlertController alertTipWithTitle:@"您当前还没有登录" message:@"请您先去登录,在来看看吧！" sure:^(UIAlertAction *action) {
//                
//                [self goToLogin];
//                
//            } cancel:^(UIAlertAction *action) {
//                
//            }];
//
//        }
//    }
//}
        //去登录
-(void)goToLogin{
    
    YCLoginViewController *loVC = [[YCLoginViewController alloc]init];
    [self.navigationController pushViewController:loVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
